﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MiscHelper
{
    public static class PhoneNumber
    {
        public static string Sanitize(string sz)
        {
            StringBuilder sb = new StringBuilder();

            if (!string.IsNullOrWhiteSpace(sz))
            {
                sz = sz.Replace(" ", "");
                sz = sz.Replace("(", "");
                sz = sz.Replace(")", "");
                sz = sz.Replace("+", "");

                if (sz.StartsWith("856"))
                {
                    sz = sz.Substring(3);
                }

                if (sz.StartsWith("0"))
                {
                    sz = sz.Substring(1);
                }
                // if the number is too short - supress it

                if (sz.Length > 6)
                {
                    sb.Append("+856 (0)");

                    int index = 0;
                    foreach (var ch in sz)
                    {
                        sb.Append(ch);
                        index++;
                        switch (index)
                        {
                            case 2:
                            case 5:
                                sb.Append(" ");
                                break;
                        }
                    }
                }
            }
            return sb.ToString().Trim();
        }
    }
}
